package com.bop.dehong.setting;

import java.util.ArrayList;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.base.dehong.http.RequestCallBack;
import com.bop.dehong.R;
import com.bop.dehong.base.BaseActivity;
import com.bop.dehong.base.DeHongApp;
import com.bop.dehong.base.TopManager;
import com.bop.dehong.config.Config;
import com.bop.dehong.http.model.GetSystemHeadModel;
import com.bop.dehong.http.model.UpDateHeadModel;
import com.bop.dehong.info.EmptyInfo;
import com.bop.dehong.info.SystemHeadInfo;
import com.bop.dehong.util.ImageLoaderUtils;

/**
 * @author TangWenhan/YeFengyun
 *2015-11-20下午5:29:45
 */
public class SetSystemAvatarActivity extends BaseActivity{
	private GridView mGridView;
	private SystemAvatarAdapter mAdapter;
	private UpDateHeadModel mUpdateHeadModel;
	private GetSystemHeadModel mGetModel;
	private String uid;
	
	@Override
	protected void initTop() {
		View mView = getView(R.id.top);
		TopManager manager = new TopManager(mView);
		manager.setTitle("设置新头像");
		manager.setLeftImageVisibility(View.VISIBLE);
	}

	@Override
	protected void initUI() {
		uid = getIntent().getStringExtra(Config.UID);
		mGridView = getView(R.id.gridView1);
	}
	
	private void updateHead(String id){
		if (mUpdateHeadModel == null) {
			mUpdateHeadModel = new UpDateHeadModel(this, 1, headCallback);
		}
		showProgressDialog();
		mUpdateHeadModel.doNet(uid, id);
	}
	RequestCallBack<EmptyInfo> headCallback = new RequestCallBack<EmptyInfo>() {

		@Override
		public void end() {
			disappearProgressDialog();
		}

		@Override
		public void onSuccess(EmptyInfo info) {
			super.onSuccess(info);
			if (!TextUtils.isEmpty(DeHongApp.mUserInfo.head)) {
				ImageLoaderUtils.getInstance(SetSystemAvatarActivity.this).clearCache();
			}
			Intent intent = getIntent();
			intent.putExtra("head", seleteInfo.thumb);
			setResult(RESULT_OK,intent);
			finishCurrentActivity();		
		}
	};
	
	@Override
	protected void initData() {
		mAdapter = new SystemAvatarAdapter(this);
		mGridView.setAdapter(mAdapter);
		mGridView.setOnItemClickListener(this);
		showProgressDialog();
		mGetModel = new GetSystemHeadModel(this, getCallback);
		mGetModel.doNet();
	}
	
	RequestCallBack<ArrayList<SystemHeadInfo>> getCallback = new RequestCallBack<ArrayList<SystemHeadInfo>>() {

		@Override
		public void end() {
			disappearProgressDialog();
		}

		@Override
		public void onSuccess(ArrayList<SystemHeadInfo> info) {
			super.onSuccess(info);
			mAdapter.addAll(info);
			mAdapter.notifyDataSetChanged();
		}
		
	};
	
	
	private SystemHeadInfo seleteInfo;


	@Override
	protected int getContentViewId() {
		TAG = getClass().getName();
		return R.layout.act_setting_avatar;
	}


	@Override
	public void onClick(View v)
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id)
	{
		seleteInfo= (SystemHeadInfo) parent.getItemAtPosition(position);
		updateHead(seleteInfo.id);
	}

}

